package com.sanpu.kxe.business.card.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.sanpu.kxe.business.card.bean.BankInfoOutput;
import com.sanpu.kxe.business.card.contract.BankCardDetailsContract;
import com.sanpu.kxe.business.card.contract.CardConfig;
import com.sanpu.kxe.business.card.model.CardModel;
import com.sanpu.kxe.business.card.provider.CardOpenProvider;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.repay.provider.IRepayProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.jump.eventbus.RefreshBankEvent;
import com.sanpu.kxe.module.dialog.CertificateDialog;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：银行卡详情
 * Created by 薛勇军 on 2018/7/25.
 */

public class BankCardDetailsPresenter implements BankCardDetailsContract.Presenter {

    private BankCardDetailsContract.View mView;

    private CompositeSubscription mSubscription;

    private BankInfoOutput infoOutput = null;

    private String mChkSts = "";

    public BankCardDetailsPresenter(BankCardDetailsContract.View mView, BankInfoOutput infoOutput, String chkSts) {
        this.mView = mView;
        this.infoOutput = infoOutput;
        this.mChkSts = chkSts;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void updateDefBank() {
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput == null) {
            return;
        }
        final String chkSts = centerOutput.getChkSts();
        if (!TextUtils.equals(CommonConfig.AUTHEN_PASS, chkSts)) {
            new CertificateDialog(mView.getContext(), new DialogViewListener() {
                @Override
                public void btnRightConfirm() {
                    if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, chkSts)) {
                        openCertificationActivity();
                    }
                }

                @Override
                public void btnLeftCancel() {

                }
            }).show();
            return;
        }
        if (TextUtils.equals(CardConfig.DEFBANK_AUDIT, mChkSts)) {
            new GlobalDialog(mView.getContext())
                    .title("审核中！")
                    .content("(后台正在审核，请耐心等待)")
                    .show();
            return;
        }
        CardOpenProvider.getInstance().openUpdateDefBankActivity(mView.getContext(), mChkSts);
    }

    /**
     * 实名认证
     */
    private void openCertificationActivity() {
        IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (provider != null) {
            provider.openCertificationActivity(mView.getContext(), true);
        }
    }

    @Override
    public void openUpdateBankTelActivity() {
        if (TextUtils.equals(CommonConfig.CREDIT_CARD_TYPE, infoOutput.getCardType())) {
            IRepayProvider provider = PublicProviderUtil.getProvider(IRepayProvider.class);
            if (provider != null) {
                provider.openUpdateCreditActivity(mView.getContext(), infoOutput);
            }
        } else {
            CardOpenProvider.getInstance().openUpdateBankTelActivity(mView.getContext(),
                    infoOutput, CommonConfig.REQUESTCODE_UPDATE_BANKTEL);
        }
    }

    @Override
    public void deleteBank() {
        DialogUtil.getInstance().dialogExistTitleTwoBtnHaveCallBack(mView.getContext(), "删除银行卡",
                "是否删除这张银行卡？", "取消", "确定", new DialogViewListener() {
                    @Override
                    public void btnRightConfirm() {
                        deleteUrmBank();
                    }

                    @Override
                    public void btnLeftCancel() {

                    }
                });
    }

    /**
     * 删除银行卡
     */
    private void deleteUrmBank() {
        mView.showLoadingDialog(true);
        Subscription subscription = CardModel.getInstance().deleteUrmBank(infoOutput.getBankNo())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        ToastUtil.showTextViewPrompt("删除银行卡成功！");
                        EventBus.getDefault().post(new RefreshBankEvent());
                        ((Activity)mView.getContext()).finish();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void updateRsdTel(String rsdTel) {
        if (infoOutput != null && !TextUtils.isEmpty(rsdTel)) {
            infoOutput.setRsdTel(rsdTel);
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
